extends "res://interactable.gd"
class_name PointBox

export var box_index:int

var _point :Point

var connect_boxes := []

signal point_changed

func add_connect_boxes(box):
	if self == box:
		return
	connect_boxes.append(box)

func set_point(point):
	if _point || point == _point:
		return
		
	_point = point
	#_point.global_position = global_position
	_point.update_point_index(box_index)
	add_child(_point)
	#set_deferred("add_child", _point)

func is_empty():
	return not _point
	
func is_valid_point() -> bool:
	if is_empty():
		return false
	return _point.is_target_box()
	
func clear():
	if _point:
		remove_child(_point)
		_point = null

func _interact():
	._interact()
	if is_empty():
		return
	for it in connect_boxes:
		if it.is_empty():
			remove_child(_point)
			it.set_point(_point)
			_point = null
			emit_signal("point_changed")
			return
